package com.bxg.lts;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

public class LtsClient {
    public static void main(String[] args) throws IOException {
        //1.创建通道
        SocketChannel socketChannel = SocketChannel.open();
        //2.连接服务器
        socketChannel.connect(new InetSocketAddress("127.0.0.1",9999));
        //3.设置非阻塞
        socketChannel.configureBlocking(false);
        //3.读取服务区的数据
        Scanner scanner=new Scanner(System.in);
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        int len;
        while ((len=socketChannel.read(byteBuffer))>0){
            byteBuffer.flip();
            System.out.println(new String(byteBuffer.array(),0,len));
            byteBuffer.clear();
        }
        String name = scanner.nextLine();

        //4.像服务器发送数据
        while (true){
            System.out.println("请输入你要发送的数据");
            String s = scanner.nextLine();
            ByteBuffer buffer = ByteBuffer.wrap((name+":"+s).getBytes());
            socketChannel.write(buffer);
        }
    }
}
